package com.cicoding.service.impl;

import java.util.List;

import org.apache.shiro.authc.CredentialsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cicoding.bean.ShiroUser;
import com.cicoding.bean.User;
import com.cicoding.constant.Constant;
import com.cicoding.dao.UserMapper;
import com.cicoding.service.UserService;
import com.cicoding.utils.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BeanUtil beanUtil;
    @Autowired
    private LinTools linTools;
    @Autowired
    private JqgridUtil jqgridUtil;

    @Override
    public List<User> getAllUser() {

        return beanUtil.toUserList(userMapper.selectList(null));
    }

    @Override
    public String addUser(String username, String avator, String dept, String role) {

        ShiroUser user = new ShiroUser();

        user.setUsername(username);
        user.setPassword(MD5Util.encrypt("111111"));
        user.setAvator(avator);
        user.setDeptid(Integer.parseInt(dept));
        user.setRoleid(Integer.parseInt(role));
        user.setSalt(linTools.getSalt());
        return userMapper.insert(user) + "";
    }

    @Override
    public String resetPassword(int id) {
        ShiroUser user = new ShiroUser();
        user.setId(id);
        user.setPassword(MD5Util.encrypt("111111"));
        int result = userMapper.updateById(user);
        return result + "";
    }

    @Override
    public String deleteUser(int id) {
        int userresult = userMapper.deleteById(id);
        return userresult + "";
    }

    @Override
    public String modifyInfo(String newId, String newDeptId, String newRoleId) {

        ShiroUser user = new ShiroUser();
        user.setId(Integer.parseInt(newId));
        user.setDeptid(Integer.parseInt(newDeptId));
        user.setRoleid(Integer.parseInt(newRoleId));
        int result = userMapper.updateById(user);
        return result + "";
    }

    @Override
    public String changePassword(String oldPassword, String newPassword) {
        ShiroUser user = userMapper.selectById(Userinfo.getUserid());

        if (!user.getPassword().equals(MD5Util.encrypt(oldPassword))) {
            return Constant.ERROR_SET_PASSWORD_NO_MATCH;
        }

        user = new ShiroUser();
        user.setId(Userinfo.getUser().getId());
        user.setPassword(MD5Util.encrypt(newPassword));
        int result = userMapper.updateById(user);
        return result + "";
    }

    @Override
    public String getAllUserJson(int page, int count) {
        List<User> all = getAllUser();
        int toIndex = count * page;
        if (all.size() < toIndex) {
            toIndex = all.size();
        }
        List<User> list = all.subList(count * (page - 1), toIndex);
        return jqgridUtil.getJson(list, page + "", all.size(), count);
    }

    public ShiroUser getUserByName(String userName) {
        List<ShiroUser> userList =  userMapper.selectByUsername(userName);
        if(userList.isEmpty()){
            System.out.println("--------------用户名错误:"+userName);
            throw new CredentialsException();
        }
        return userList.get(0);
    }
}
